package com.hongchen.wechat.controller;

import com.hongchen.model.ActGame;
import com.hongchen.model.ActGamePrize;
import com.hongchen.model.ActUser;
import com.hongchen.wechat.config.Api;
import com.hongchen.wechat.service.RichService;
import com.jfinal.weixin.sdk.api.ApiResult;


import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 任文旭 on 2018/1/18.
 *
 * 介绍：找第字
 */
public class FindDiController extends Api {
    private final RichService richService = enhance(RichService.class);
    @Override
    public ApiResult userdetail() {
        return super.userdetail();
    }
    public void index() {
        //这里定义一个对象为了更好的获取用户的个人信息
        ApiResult info = userdetail();
        System.out.println(info);
        String openid = info.get("openid");
        setSessionAttr("openid", openid);
        String nickname = info.get("nickname");
        /**
         * 判断用户刷新以后openid是否为空
         * 如果为空的话重新拉去用户信息
         *
         */
        if (openid == null) {
            this.redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId()+"&redirect_uri=http://"+chengDong()+".tunnel.echomod.cn/finddi&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect");
        } else {
            /**
             *  这里还要查询数据库中是否已经存在这个用户，如果已经存在opneid
             *  如果 存在就直接跳转不存在就说明表用户是新用户存贮用户的信息
             */
            ActUser act = richService.findByOpenid(openid);
            if (act == null) {
                //存储用户信息
                ActUser actUser = new ActUser();
                actUser.setCreateTime(new Date());
                actUser.setHeadImg((String) info.get("headimgurl"));
                actUser.setWechatOpenid(openid);
                actUser.setWechatNickname(nickname);
                actUser.setSex((Integer) info.get("sex"));
                actUser.save();
                ActGame actGame = new ActGame();
                actGame.setActUserId(actUser.getId());
                actGame.setSieveChance((long) 5);
                actGame.setNowTime(new Date());
                actGame.save();
            }
            //用户参数
            System.out.println("微信名称：" + info.get("nickname"));
            //setAttr("num",2);//找弟字时间倒计时
            render("finddi.html");
        }
    }

    public void findfinum() {
        String cishu = getPara("cishu");
        System.out.println("找到几个弟字" + cishu);
        int ci = Integer.parseInt(cishu);
        /**
         *  从session中查询目前的登录用户openid
         *  然后去查询用户在游戏表中的id
         */
        String openid = getSessionAttr("openid");
        System.out.println("openid" + openid);
        //使用用户的openid从用户表中查询用户信息
        ActUser actUser = richService.findByOpenid(openid);
        if (ci >= 0 && ci < 20) {
            //为了测试用的方法，这是通关以后执行的方法，正式上线的时候是需要删除的
         /*   richService.updateSieveChanceTwo(actUser.getId());
            ActGamePrize actGamePrize = new ActGamePrize();
            String sopenid = getSessionAttr("openid");
            ActGame actGame = richService.findActId(sopenid);
            actGamePrize.setGameId(actGame.getId());
            actGamePrize.setPrizeId((long) 6);
            //生成奖品编码
            String no= richService.lastPrizeNo();
            setSessionAttr("no",no);
            actGamePrize.setPrizeNum(no);
            actGamePrize.setIsRecive(0);
            actGamePrize.setAwardTime(new Date());
            actGamePrize.save();*/


            //数据库没有操作
            System.out.println("阿斯顿发送到发送到发送到发送到发送到发送");
        } else if (ci >= 20 && ci < 60) {
            richService.updateSieveChance(actUser.getId());
        } else if (ci >= 60) {
            richService.updateSieveChanceTwo(actUser.getId());
            ActGamePrize actGamePrize = new ActGamePrize();
            String sopenid = getSessionAttr("openid");
            ActGame actGame = richService.findActId(sopenid);
            actGamePrize.setGameId(actGame.getId());
            actGamePrize.setPrizeId((long) 6);
            //生成奖品编码
           /* SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); // 时间字符串产生方式
            String uid_pfix = "NO" + format.format(new Date()); // 组合流水号前一部分，NO+时间字符串，如：NO20160126
            String no = uid_pfix + "" + (int) ((Math.random() * 9 + 1) * 10000000);*/
            String no= richService.lastPrizeNo();
            if(no==null){
                Double a = Math.random() * 1000000;
                int b = 1 + (int) (a + 1);
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
                no = "MU"+format.format(new Date()) + b;
            }
            setSessionAttr("no",no);
            actGamePrize.setPrizeNum(no);
            actGamePrize.setIsRecive(0);
            actGamePrize.setAwardTime(new Date());
            actGamePrize.save();
            setAttr("pri", "哈哈哈哈哈");
        }
        System.out.println(actUser.getId());
        ActGame actGame = richService.finds(actUser.getId());
        System.out.println(actGame);
        renderJson();
    }
    /*当用户达到60关的时候向前台推送奖品价值和奖品编号*/
    public void tests() {
        int cc = getParaToInt("a");
        System.out.println(cc);
        if (cc > 5) {
            String no=getSessionAttr("no");
            //这里为了避免session中的编号为null的时候执行后台重新查询
            if(no==null){
                String openid=getSessionAttr("openid");
                ActGamePrize actGamePrize= richService.findPrize(openid);
                String bian=actGamePrize.getPrizeNum();
                setAttr("ss",200);
                setAttr("bian",bian);
            }
            setAttr("ss", 200);//奖品价值
            setAttr("bian", no);//奖品编号
        }
        renderJson();
    }
}
